package control.interceptor.impl;

import base.interactive.redis.RedisClusterUtils;
import base.standard.constant.ErrorCode;
import base.standard.constant.TokenKey;
import base.standard.exception.SimpleException;
import control.interceptor.TokenProcesses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * Token core processes
 *
 * @description:
 * @author: cyj
 * @date 2020 -10-29 17:17:53
 */
@Service("tokenCoreProcesses")
public class TokenCoreProcesses implements TokenProcesses {

    /**
     * Redis cluster utils
     */
    @Autowired
    private RedisClusterUtils redisClusterUtils;

    @Override
    public void processes(String loginId) {
        String ssoKey = TokenKey.USER_SSO_HASH.key + loginId;
        // SSO处理(这个值登陆的时保存过期时间半小时), 无返回为登陆过期
        Optional.ofNullable(redisClusterUtils.get(ssoKey)).orElseThrow(
                () -> new SimpleException(ErrorCode.TOKEN_EXPIRE));
        // 持续操作更新有效期
        redisClusterUtils.expire(ssoKey, TokenKey.USER_SSO_HASH.expire);
    }
}
